package com.hcbd.tdwt.ui.xingyu.adapter;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hcbd.tdwt.R;

import java.util.List;
public class BluetoothAdapter extends RecyclerView.Adapter<BluetoothAdapter.MyViewHolder> {

    protected final Context mContext;
    private List<BluetoothDevice> blueToothList;
    private final LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    private boolean isShowInfo;
    public BluetoothAdapter(Context context, List<BluetoothDevice> list,boolean isShowInfo) {
        mContext = context;
        mInflater = ((Activity) context).getLayoutInflater();
        blueToothList = list;
        this.isShowInfo=isShowInfo;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.bluetooth_item_xingyu, viewGroup, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        BluetoothDevice bluetoothDevice = blueToothList.get(i);
        if(isShowInfo){
            myViewHolder.infoImg.setVisibility(View.VISIBLE);
            myViewHolder.infoImg.setOnClickListener(view -> onItemClickListener.onInfo(bluetoothDevice));
        }else{
            myViewHolder.infoImg.setVisibility(View.GONE);
        }
        myViewHolder.bluetoothName.setText(bluetoothDevice.getName());
        myViewHolder.adders.setText(bluetoothDevice.getAddress());

        myViewHolder.areaLL.setOnClickListener(v -> onItemClickListener.onClick(bluetoothDevice));
    }

    @Override
    public int getItemCount() {
        return blueToothList.size();
    }

    public void notifyDataSetChanged(List<BluetoothDevice> list) {
        blueToothList = list;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView bluetoothName, adders;
        RelativeLayout areaLL;
        ImageView infoImg;
        MyViewHolder(View itemView) {
            super(itemView);
            bluetoothName = itemView.findViewById(R.id.bluetooth_name);
            adders = itemView.findViewById(R.id.bluetooth_adders);
            areaLL = itemView.findViewById(R.id.areaLL);
            infoImg=itemView.findViewById(R.id.bluetooth_info_img);
        }
    }

    public interface OnItemClickListener {
        void onClick(BluetoothDevice bluetoothDevice);
        void onInfo(BluetoothDevice bluetoothDevice);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}